var DAO = require('./DAO')
var carListDao = {
    ShowCarDetails: function (carId, callback) {
        DAO('select * from cars where carId =?', [carId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    ShowBrandCars: function (brand, callback) {
        DAO('select * from cars where brand  =?', [brand], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowPlimitsCars: function (price, callback) {
        DAO('select * from cars where price>? && price<? ', [price.lowPrice, price.highPrice], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowDisplacement: function (displacement, callback) {
        DAO('select * from cars where displacement > ? && displacement < ?  ', [displacement.displacement1, displacement.displacement2], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowTypeCars: function (carType, callback) {

        DAO('select * from cars where carType =?', [carType], function (err, results) {

            if (err) {
                console.log(err.message)
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowPriceSortUp: function (callback) {
        DAO('select * from cars order by price asc ', null, function (err, results) {

            if (err) {
                console.log(err.message)
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowPriceSortDown: function (callback) {
        DAO('select * from cars order by price desc ', null, function (err, results) {

            if (err) {
                console.log(err.message)
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    ShowSalesSort: function (callback) {
        DAO('select * from cars order by carsale desc    ', null, function (err, results) {

            if (err) {
                console.log(err.message)
                callback(err, null)
            } else {
                console.log(results)
                callback(null, results)
            }
        })
    },
    SearchkeyWord: function (type1, callback) {
        DAO("call listcar(?)", [type1],function (err, results) {
            if (err) {
                console.log(err.message)
                callback(err)
            } else {
                callback(null, results)
            }
        })
    },
    SearchAllKey: function (cars, callback) {
        console.log('多条件查询', cars)
        // 默认的查询，价格：0-1000；排量：0-1000，按价格升序排序
        if (cars.type == 4) {
            DAO("CALL car4(?,?,?,?,?)", [cars.order, cars.price1, cars.price2, cars.displacement1, cars.displacement2], function (err, results) {
                if (err) {
                    console.log(err.message)
                    callback(err)
                } else {
                    callback(null, results)
                }
            })
        }
        // 默认的查询，价格：0-1000；排量：0-1000，按价格升序排序，查询汽车的品牌
        if (cars.type == 3) {
            DAO("CALL car3(?,?,?,?,?,?)", [cars.order, cars.price1, cars.price2, cars.displacement1, cars.displacement2, cars.brand], function (err, results) {
                if (err) {
                    console.log(err.message)
                    callback(err)
                } else {
                    callback(null, results)
                }
            })
        }
        // 默认的查询，价格：0-1000；排量：0-1000，按价格升序排序，查询汽车的类型
        if (cars.type == 2) {
            DAO("CALL car2(?,?,?,?,?,?)", [cars.order, cars.price1, cars.price2, cars.displacement1, cars.displacement2, cars.carType], function (err, results) {
                if (err) {
                    console.log(err.message)
                    callback(err)
                } else {
                    callback(null, results)
                }
            })
        }
        // 默认的查询，价格：0-1000；排量：0-1000，按价格升序排序，查询汽车的品牌，查询汽车类型
        if (cars.type == 1) {
            DAO("CALL car1(?,?,?,?,?,?,?)", [cars.order, cars.price1, cars.price2, cars.brand, cars.displacement1, cars.displacement2, cars.carType], function (err, results) {
                if (err) {
                    console.log(err.message)
                    callback(err)
                } else {
                    callback(null, results)
                }
            })
        }

    },


}
module.exports = carListDao